﻿window.requestAnimFrame = (function () {
    return window.requestAnimationFrame ||
			window.webkitRequestAnimationFrame ||
			window.mozRequestAnimationFrame ||
			window.oRequestAnimationFrame ||
			window.msRequestAnimationFrame ||
			function (/* function */ callback, /* DOMElement */ element) {
			    console.log("using timeout fallback");
			    return window.setTimeout(callback, 1000 / 60);
			};
})();

var i=0;
animate = function (animationStep) {
    var self = this;
    return window.requestAnimFrame(animationStep);
}

var animation = {
    interval: null,
    observers: [],    
    bind: function (obj,fnc) {
        if (typeof (fnc) == "function") {
            this.observers.push({
                obj: obj,
                fnc : fnc
            });
        }
    },
    unbind: function (fnc) {       
        animation.observers.find(function(elem,index,arr){
            if(elem[fnc]=fnc)
               arr.splice(index,1);
        });
    },
    onAnimationFrame: function (applicationTime) {        
        for (var i = 0; i < animation.observers.length; i++) {
            
            animation.observers[i].fnc(animation.observers[i].obj,applicationTime);
        }        
        animation.interval = animate(animation.onAnimationFrame);
    },
    start: function () {
        animation.interval = animate(this.onAnimationFrame);
    },
    stop: function () {
        console.log("not implemented")
    }
};
animation.start();
